package com.metaera.app.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.lxj.xpopup.XPopup;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.CategoryPresenter;
import com.metaera.app.controller.presenter.SearchHotPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.controller.view.HomeCategoryView;
import com.metaera.app.controller.view.SearchHotMarqueeView;
import com.metaera.app.ui.activities.MessageCenterActivity;
import com.metaera.app.ui.activities.SearchActivity;
import com.metaera.app.ui.core.BaseFragment;
import com.metaera.app.ui.popup.HomeCategoryPopup;
import com.metaera.app.utils.ViewUtils;
import com.metaera.app.utils.event.OnLauncherCalendarEvent;
import com.sunfusheng.marqueeview.MarqueeView;

import net.lucode.hackware.magicindicator.MagicIndicator;

import org.greenrobot.eventbus.EventBus;

import androidx.viewpager.widget.ViewPager;

public class HomeFragment extends BaseFragment implements View.OnClickListener {

    private HomeCategoryPopup mHomeCategoryPopup;

    @Override
    protected int getLayoutId() {

        return R.layout.fragment_newest_home;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        ViewUtils.addStatusBarHeightPadding(findViewById(R.id.layout_container));

        // find
        ViewPager viewPager = findViewById(R.id.vp_pager);
        MagicIndicator magicIndicator = findViewById(R.id.magic_indicator);
        MarqueeView<String> marqueeView = findViewById(R.id.marquee_view);

        // init
        mHomeCategoryPopup = new HomeCategoryPopup(viewPager.getContext(), viewPager);

        // click listener
        findViewById(R.id.layout_search).setOnClickListener(this);
        findViewById(R.id.button_home_menu).setOnClickListener(this);
        findViewById(R.id.iv_message_tag).setOnClickListener(this);
        findViewById(R.id.iv_calendar_tag).setOnClickListener(this);

        //magicIndicator.getNavigator().onPageSelected();

        // network 分类
//        RequestPresenter<INetworkRequestView<List<HomeNewsTypeResponse>>> presenter =
//                PresenterFactory.get(new CategoryPresenter(), null, getLifecycle());

        CategoryPresenter categoryPresenter = new CategoryPresenter();
        categoryPresenter.setModelAndView(new BasicNetworkRequestView<>(new HomeCategoryView(
                getChildFragmentManager(), viewPager, magicIndicator)));
        categoryPresenter.home();

        // 热门搜索
        SearchHotPresenter hotSearchPresenter = new SearchHotPresenter();
        hotSearchPresenter.setModelAndView(new SearchHotMarqueeView(marqueeView));
        hotSearchPresenter.hotSearch();
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.layout_search) {
            startActivity(new Intent(getContext(), SearchActivity.class));

        } else if (v.getId() == R.id.button_home_menu) {
            new XPopup.Builder(v.getContext()).enableDrag(false).asCustom(mHomeCategoryPopup).show();

        } else if (v.getId() == R.id.iv_message_tag) {
            startActivity(MessageCenterActivity.class);

        } else if(v.getId() == R.id.iv_calendar_tag){
            EventBus.getDefault().post(new OnLauncherCalendarEvent());
        }
    }

}
